package com.dubbo.microserviceweb.controller;

import com.dubbo.api.PaperService;
import com.dubbo.api.model.BrowserModel;
import com.dubbo.api.model.PaperModel;
import com.dubbo.microserviceweb.controller.viewobject.PaperVO;
import com.dubbo.microserviceweb.error.BussinessException;
import com.dubbo.microserviceweb.error.EmBussinessError;
import com.dubbo.microserviceweb.response.CommonReturnType;
import com.dubbo.provider.entity.Browser;
import com.dubbo.provider.entity.Paper;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.format.DateTimeFormat;
import org.omg.CORBA.COMM_FAILURE;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Controller("/paper")
@RequestMapping("/paper")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class PaperController extends BaseController {
    @Reference
    private  PaperService paperService;


    //论文浏览
    @RequestMapping(value = "/list")
    @ResponseBody
        public CommonReturnType listPaper()  {
        //调用service获取论文对象并返回前端
        List<PaperModel> paperModelList = paperService.listPaper();

        //将核心领域模型论文对象转化为可供UI使用的viewobject
        //使用stream api将list内的paperModel转化为paperVO
        List<PaperVO> paperVOList =paperModelList.stream().map(paperModel -> {
            PaperVO paperVO = this.convertVOFromModel(paperModel);
            return paperVO;
        }).collect(Collectors.toList());
        //论文记数
        PaperModel paperModel = new PaperModel();
        paperModel =paperService.count();
        return CommonReturnType.createPage(paperVOList,paperModel.getCount());
    }
    private PaperVO convertVOFromModel(PaperModel paperModel){
        if(paperModel== null){
            return null;
        }
        PaperVO paperVO = new PaperVO();
        BeanUtils.copyProperties(paperModel,paperVO);
        //设置时间格式
        SimpleDateFormat std = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        paperVO.setpDate(std.format(paperModel.getpDate()));
        return paperVO;
    }
}
